
package org.vectrics.user.dataweb;

import org.vectrics.common.dom.MarshalUtils;
import org.vectrics.company.Company;
import org.vectrics.dataweb.WebServiceAction;
import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.SystemException;
import org.vectrics.VectricsException;
import org.apache.log4j.Logger;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.struts.Request;
import org.vectrics.user.Person;
import org.vectrics.user.PersonService;
import org.vectrics.user.UserSummary;
import org.vectrics.user.PersonSearchCriteria;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;



public class NameSearchAction extends WebServiceAction {
	private transient static Logger log = Logger.getLogger(NameSearchAction.class);
	
	/**
	 * 
	 */
	public NameSearchAction() {
		super();
	}

	public void getData(Request request, String command, Element requestData, 
					Element dataElement) {
						
		Element searchElement = null;
		String userId = null;
		log.debug("getData() - put user details in document");
		NodeList criteriaList = requestData.getElementsByTagName("user-criteria");
		if (criteriaList.getLength() > 0) {
			searchElement = (Element)criteriaList.item(0); 
		}
		else {
			throw new SystemException("Request element did not have a 'user-criteria' child element");
		}

						
						
		log.debug("getData() - put user list information in document");
		String lastName = searchElement.getAttribute("lastName");
		String firstName = searchElement.getAttribute("firstName");
		PersonSearchCriteria criteria = CoreServiceLocator.createPersonSearchCriteria();
		criteria.setFirstName(firstName);
		criteria.setLastName(lastName);
		
		Set domains = CoreServiceLocator.getPersonService()
			.findDomainsWithUserPrivilege(ThreadProperties.getPerson(), Person.PRIVILEGE_PERSON_VIEW);
		criteria.setDomains(domains);
		
		log.debug("firstName = " + firstName);
		log.debug("lastName = " + lastName);
		
		Collection ids = CoreServiceLocator.getPersonService().findPersons(criteria, 0, 40);
		log.debug("getData() - user id count = " + ids.size());
		Collection users = CoreServiceLocator.getPersonService().loadUsers(ids);
		Element userListElement = this.createChildElement(dataElement, "userList");
		
		Iterator iter = users.iterator();
		while (iter.hasNext()) {
			log.debug("processing next user");
			Person user = (Person)iter.next();
			UserSummary userSummary = new UserSummary();
			userSummary.setId(user.getId());
			userSummary.setUserName(user.createUserIfNotExist().getUserName());
			userSummary.setFirstName(user.getFirstName());
			userSummary.setLastName(user.getLastName());
			userSummary.setMiddleName(user.getMiddleName());
			MarshalUtils marshaller = MarshalUtils.getInstance();
			marshaller.objectToElement(userListElement, userSummary, "userSummary");
		}				
	}




}
